package com.example.a2032.activity;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.a2032.R;
import com.example.a2032.Utils.Utils;
import com.hyphenate.util.DateUtils;


import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SmsShowActivity extends BaseActivity {

    @BindView(R.id.tv_contactName)
    TextView tvContactName;
    @BindView(R.id.tvSmsTime)
    TextView tvSmsTime;
    @BindView(R.id.tv_smsContent)
    TextView tvSmsContent;
    public static final String KEY_SmsSender = "SmsSender";
    public static final String KEY_SmsInfo = "SmsInfo";
    long smsTime=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms_show);
        ButterKnife.bind(this);
        init(getIntent());
    }

    void init(Intent intent) {
        Utils.wakeUpAndUnlock(this);
        Utils.startVibrato(this,500);
        String sender = intent.getStringExtra(KEY_SmsSender);
        String info = intent.getStringExtra(KEY_SmsInfo);
        tvContactName.setText(getShowContact(sender));
        tvSmsContent.setText(info);
        tvSmsContent.setMovementMethod(new ScrollingMovementMethod());
        smsTime=System.currentTimeMillis();
    }

    public String getShowContact(String phoneNumber) {
        String name = "";
        Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/" + phoneNumber);
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(uri, new String[]{ContactsContract.Data.DISPLAY_NAME}, null, null, null);
        if (cursor == null) {
            return phoneNumber;
        }
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            int nameFieldColumnIndex = cursor
                    .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
            cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
            name = cursor.getString(nameFieldColumnIndex);
        }
        if (cursor != null) {
            cursor.close();
        }
        cursor.close();
        return TextUtils.isEmpty(name) ? phoneNumber : name;
    }

    private static final String TAG = "SmsShowActivity";
    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
        tvSmsTime.setText(getInfoTime());
    }

    String getInfoTime() {
        String infoTime = "";
        long period=System.currentTimeMillis()-smsTime;
        if(period<60*1000){
            infoTime="刚刚";
        }else if(period<60*60*1000){
            int minute= (int) (period/(60*1000));
            infoTime=minute+"分钟之前";
        }else{
            Date date=new Date();
            date.setTime(smsTime);
            infoTime= DateUtils.getTimestampString(date);
        }
        return infoTime;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.i(TAG, "onNewIntent: ");
        init(intent);
    }
}